﻿using Barkod.Utility;
using DevExpress.XtraGrid.Views.Grid;
using Extensions;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Barkod
{
    public partial class Oturum : XPopupForm
    {
        public int BarkodSayId = 0;
        public string FileName;

        public Oturum()
        {
            InitializeComponent();
        }

        private void Users_Load(object sender, EventArgs e)
        {
            System.Data.DataTable dt = AppSession.DataManager.ExecuteDataTable("GetBarkodSay");
            gvMaster.Init(dt);
            gvMaster.FormatAsDate("Tarih");
            gvMaster.SetCaptions("BarkodSayId:Oturum No.", "Aciklama:Açıklama", "Olusturan:Oluşturan");
            gvMaster.MakeColumnInvisible("UserId");
            gvMaster.AddSummary();
            gvMaster.BestFitColumns();
            gvMaster.RowStyle += gvMaster_RowStyle;
            gvMaster.DoubleClick += gvMaster_DoubleClick;
            gvMaster.OptionsView.EnableAppearanceEvenRow = false;
        }

        void gvMaster_RowStyle(object sender, RowStyleEventArgs e)
        {
            if (e.RowHandle == gvMaster.FocusedRowHandle)
                e.Appearance.BackColor = Color.YellowGreen;
        }



        void gvMaster_DoubleClick(object sender, EventArgs e)
        {
            BarkodSayId = gvMaster.GetFocusedRowCellDisplayText("BarkodSayId").ToIntNN();
            SetDosya();
        }

        private void SetDosya()
        {
            if (BarkodSayId > 0)
            {
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
                    ofd.Filter = "Mağaza Dağılımı Dosyaları|*.xlsx";
                    if (ofd.ShowDialog() == DialogResult.OK)
                        FileName = ofd.FileName;
                    else
                        this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
                }

                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }


        private void btnKaydet_Click(object sender, EventArgs e)
        {
            BarkodSayId = (int)AppSession.DataManager.ExecuteScalar("UpdBarkodSay", txtAd.Text, WhoAmI);
            SetDosya();
        }
    }
}
